package common

import (
	"encoding/binary"
)

const (
	MagicValue   = 0xDEADBEEF
	VersionValue = 1
	HeaderSize   = 16 // 头部大小(4+2+2+4+4)
	//ServerAddress = "127.0.0.1:9999"
)

const (
	CmdNone      = 0
	CmdData      = 1
	CmdJson      = 2
	CmdHeartbeat = 3
)

// 协议头部结构体
type PacketHeader struct {
	Magic    uint32
	Version  uint16
	Command  uint16
	DataLen  uint32
	Reserved uint32
}

func PackData(command uint16, data []byte) []byte {
	header := PacketHeader{
		Magic:    MagicValue,
		Version:  VersionValue,
		Command:  command,
		DataLen:  uint32(len(data)),
		Reserved: 0,
	}

	buf := make([]byte, 16+len(data))
	binary.BigEndian.PutUint32(buf[0:4], header.Magic)
	binary.BigEndian.PutUint16(buf[4:6], header.Version)
	binary.BigEndian.PutUint16(buf[6:8], header.Command)
	binary.BigEndian.PutUint32(buf[8:12], header.DataLen)
	binary.BigEndian.PutUint32(buf[12:16], header.Reserved)
	copy(buf[16:], data)

	return buf
}
